<?php

class Application_Form_System extends Application_Form_Basic
{

    public function init()
    {
        $this ->setMethod ( 'post' );
        
        $this->addElement(
            'hidden', 
            'id', 
            array(
                'value' => null
            )
        );
        
		$this->addElement ( 
            'text', 
            'name', 
            array ( 
                'label' => 'Назва :',
                'required' => true,
                'validators' => array(
                    array ('NotEmpty', true, 
                        array (
                            'messages' => array (
                                'isEmpty' => 'Поле не може бути пустим!' 
                            )
                        ) 
                    ),
                    array('stringLength', false, array(1, 180)),
                    array('Db_NoRecordExists', false, array(
                        'table' => 'price_systems', 
                        'field' => 'name'
                    ))
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'name' ),
			'name_group'
		);
        
        $group = $this->getDisplayGroup ('name_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text',
            'price_cornice',
            array ( 
                'label' => 'Ціна карниза 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'price_cornice' ),
			'price_cornice_group'
		);
        
        $group = $this->getDisplayGroup ('price_cornice_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $group = $this->getDisplayGroup ('name_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text',
            'cost_cornice',
            array ( 
                'label' => 'Собівартість карниза 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'cost_cornice' ),
			'cost_cornice_group'
		);
        
        $group = $this->getDisplayGroup ('cost_cornice_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'price_threshold', 
            array ( 
                'label' => 'Ціна порога 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'price_threshold' ),
			'price_threshold_group'
		);
        
        $group = $this->getDisplayGroup ('price_threshold_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'cost_threshold', 
            array ( 
                'label' => 'Собівартість порога 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'cost_threshold' ),
			'cost_threshold_group'
		);
        
        $group = $this->getDisplayGroup ('cost_threshold_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'price_top_bar', 
            array ( 
                'label' => 'Ціна верхньої планки 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'price_top_bar' ),
			'price_top_bar_group'
		);
        
        $group = $this->getDisplayGroup ('price_top_bar_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'cost_top_bar', 
            array ( 
                'label' => 'Собівартість верхньої планки 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'cost_top_bar' ),
			'cost_top_bar_group'
		);
        
        $group = $this->getDisplayGroup ('cost_top_bar_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'price_bottom_bar', 
            array ( 
                'label' => 'Ціна нижньої планки 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'price_bottom_bar' ),
			'price_bottom_bar_group'
		);
        
        $group = $this->getDisplayGroup ('price_bottom_bar_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'cost_bottom_bar', 
            array ( 
                'label' => 'Собівартість нижньої планки 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'cost_bottom_bar' ),
			'cost_bottom_bar_group'
		);
        
        $group = $this->getDisplayGroup ('cost_bottom_bar_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'price_separator', 
            array ( 
                'label' => 'Ціна H-профілю 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'price_separator' ),
			'price_separator_group'
		);
        
        $group = $this->getDisplayGroup ('price_separator_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'cost_separator', 
            array ( 
                'label' => 'Собівартість H-профілю 1(м) :',
                'required' => true,
                'validators' => array(
                    array('Float', false)
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'cost_separator' ),
			'cost_separator_group'
		);
        
        $group = $this->getDisplayGroup ('cost_separator_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement('file', 'photos', array(
			'label' => 'Текстура :',
			'destination' => Application_Model_PathsService::getInstance()->get("temp"),
			'valueDisabled' => true,
			'Count' => array(1)
		));
        
        $this->addDisplayGroup(
			array( 'photos' ),
			'photos_group'
		);
        
        
        $group = $this->getDisplayGroup ('photos_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement ( 
            'text', 
            'system_color', 
            array ( 
                'label' => 'Колір :',
                'required' => true,
                'id' => 'systemColorPicker'
            )
        );
        
        $this->addDisplayGroup(
			array( 'system_color' ),
			'system_color_group'
		);
        
        
        $group = $this->getDisplayGroup ('system_color_group');
        $group->removeDecorator ('DtDdWrapper');
        
        $this->addElement (
            'submit', 
            'submit', 
            array (
                'ignore' => true, 
                'label' => 'Добавити' 
            ) 
        );
        
        $this->addDisplayGroup(
			array( 'submit' ),
			'buttos_group',
            array(
                'class' => 'group_buttons'
            )
		);
        
        $group = $this->getDisplayGroup ('buttos_group');
        $group->removeDecorator ('DtDdWrapper');
    }
    
    public function processUploadedFiles() {
		/*@var $adapter Zend_File_Transfer_Adapter_Http */
		$adapter =  $this->photos->getTransferAdapter();
		$receivingOK = true;
		foreach ($adapter->getFileInfo() as $file => $info) {
		    if($info['name']){
		        $originalFilename = pathinfo($info['name']);
			    $adapter->addFilter('Rename', md5(uniqid(rand(),1)) . '-' . substr($originalFilename['filename'], 0, 10) . '.' . $originalFilename['extension'], $file);
			    if (!$adapter->receive($file)) {
			         $receivingOK = false;
			    }
		    }
		}

		if(!$receivingOK){
			throw new Exception('Error uploading files!');
		}

		$this->photos->receive();
	}
}

